﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using ExcellentCRM.DataService.Models;

namespace ExcellentCRM.DataService
{
    public class PreAppStart
    {
        public static void Start()
        {
            var dsInitializer = new DataServiceInitializer();
            Database.SetInitializer(dsInitializer);

            using (var dbCtx = new DataServiceContext())
            {
                dbCtx.Database.CreateIfNotExists();
                dsInitializer.InitSetDefaultValue(dbCtx);
                InitDepartment(dbCtx);
                InitRole(dbCtx);
                InitAcct(dbCtx);
                InitMenu(dbCtx);
            }
        }
 
        private static void InitDepartment(DataServiceContext dbCtx)
        {
            var depts = new[] {"认证部", "大客户部", "教务部", "市场部", "财务部"};
            foreach (var deptStr in depts)
            {
                var dept = dbCtx.Departments.FirstOrDefault(p => p.DeptName == deptStr);
                if (dept == null)
                    dbCtx.Departments.Add(new Department {DeptName = deptStr});
            }
            dbCtx.SaveChanges();
        }

        private static void InitRole(DataServiceContext dbCtx)
        {
            var roles = new[] {"认证部", "大客户部", "教务部", "市场部", "财务部", "Admin", "Student"};
            foreach (var roleStr in roles)
            {
                var role = dbCtx.Roles.FirstOrDefault(p => p.RoleName == roleStr);
                if (role == null)
                    dbCtx.Roles.Add(new Role {RoleName = roleStr});
            }
            dbCtx.SaveChanges();
        }

        private static void InitAcct(DataServiceContext dbCtx)
        {
            var adminAcct = new Account
                {
                    AcctName = "admin",
                    AcctPass = "admin",
                    AcctType = AccountType.Admin
                };

            if (!dbCtx.Accounts.Any(p => p.AcctName == "admin" && p.AcctType == AccountType.Admin))
                dbCtx.Accounts.Add(adminAcct);
            dbCtx.SaveChanges();
        }

        private static void InitMenu(DataServiceContext dbCtx)
        {
            var menus =
                new List<Menu>
                    {
                        new Menu {MenuName = "用户管理", ActName = "Manage", CtlName = "Account"},
                        new Menu {MenuName = "保存用户", ActName = "CreateOrUpdate", CtlName = "Account", MenuType = MenuType.Action},
                        new Menu {MenuName = "部门管理", ActName = "Index", CtlName = "Department"},
                        new Menu {MenuName = "保存部门", ActName = "CreateOrUpdate", CtlName = "Department", MenuType = MenuType.Action},
                        new Menu {MenuName = "权限管理", ActName = "Index", CtlName = "Role"},
                    };
            foreach (var menu in menus)
            {
                dbCtx.Menus.Add(menu);
            }
            dbCtx.SaveChanges();
        }
    }
}